<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/8/3
 * Time: 10:53
 */

namespace plugin\admin\app\model\withdraw;


use app\model\user\UserModel;
use plugin\admin\app\model\Base;

class TransferOrderModel extends Base
{
    protected $table = 'video_spread_transfer_order';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function user()
    {
        return $this->belongsTo(UserModel::class,'spread_user_sn','spread_user_sn')->where(['isdel'=>0]);
    }

    public function index($where, $field=[],$order = [])
    {
        $query =  $this->getWhere($where);

        $field && $query->select($field);

        $data = $this->buildPage($query);
        $order && $query->orderBy($order[0],$order[1]);

        $data['data'] = $query->get();

        return $data;
    }

    public function getDetail($where,$field=[])
    {
        $query = $this->where($where);
        $field && $detail = $query->select($field);
        $detail =  $query->first();

        return $detail;
    }

    public function saveData($param)
    {
        if(!empty($param['id'])){
            $info = $this->find($param['transfer_order_id']);
            if($info==false){
                throw new UserException(4020,'转账单不存在');
            }

            $data = [];
            if($param['action']=='edit'){
                !empty($param['spread_user_sn']) && $data['spread_user_sn'] = $param['spread_user_sn'];
                !empty($param['payee_name']) && $data['payee_name'] = $param['payee_name'];
                !empty($param['payee_account']) && $data['payee_account'] = $param['payee_account'];
                !empty($param['payee_idcard']) && $data['payee_idcard'] = $param['payee_idcard'];
                isset($param['amount']) && $data['amount'] = $param['amount'];
                isset($param['transfer_type']) && $data['transfer_type'] = $param['transfer_type'];
                isset($param['submit_order_user']) && $data['submit_order_user'] = $param['submit_order_user'];
                isset($param['remark']) && $data['remark'] = $param['remark'];

            }else{
                !empty($param['checked_fail_reason']) && $data['checked_fail_reason'] = $param['checked_fail_reason'];
                !empty($param['submit_yun_fail_reason']) && $data['submit_yun_fail_reason'] = $param['submit_yun_fail_reason'];
                !empty($param['transfer_fail']) && $data['transfer_fail'] = $param['transfer_fail'];
                !empty($param['checked_time']) && $data['checked_time'] = $param['checked_time'];
                !empty($param['submit_yun_time']) && $data['submit_yun_time'] = $param['submit_yun_time'];
                !empty($param['transfering_time']) && $data['transfering_time'] = $param['transfering_time'];
                !empty($param['transfered_time']) && $data['transfered_time'] = $param['transfered_time'];
            }


            return $this->where(['id'=>$param['id']])->update($data);
        }else{
            $data = [];
            if(empty($data['transfer_order_sn'])){
                $maxId =  $this->where([['transfer_order_id','>',0]])->max('transfer_order_sn');
                $data['transfer_order_sn'] = Common::getSnCode($maxId,'80',7,2);
            }

            $data['spread_user_sn'] = $param['spread_user_sn'];
            $data['payee_name'] = $param['payee_name'];
            $data['payee_account'] = $param['payee_account'];
            $data['payee_idcard'] = $param['payee_idcard'];
            $data['amount'] = $param['amount'];
            $data['transfer_type'] = $param['transfer_type'];
            $data['remark'] = $param['remark']??'';

            $id = $this->insertGetId($data);

            return $id;
        }
    }
}